/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月25日         cassie
 */

package com.coocaa.launcher.pattern.normal.desktop;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.content.Context;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.View;

import com.coocaa.launcher.EasyLauncherActivity;
import com.coocaa.launcher.framework.launcherhost.LauncherHostView.LauncherHostPageView;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppItemData;
import com.coocaa.launcher.pattern.normal.NormalController;
import com.coocaa.launcher.pattern.normal.NormalController.BoundaryDetector;
import com.coocaa.launcher.pattern.normal.NormalController.HotSeatExecutor;
import com.coocaa.launcher.pattern.normal.UIConfig;
import com.coocaa.launcher.pattern.normal.guide.GuideView.GuideViewManager;
import com.coocaa.launcher.pattern.normal.hotseat.HotSeatView;
import com.coocaa.launcher.pattern.normal.pick.PickResultListener;
import com.coocaa.launcher.sidebar.setting.item.backexit.BackExit;
import com.coocaa.launcher.util.Utils;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.service.XServiceManager;

import java.util.List;

public class DesktopView extends LauncherHostPageView {
    private HotSeatView hotSeat;

    public DesktopView(Context context) {
        super("NormalDesktopView", context);
        // TODO Auto-generated constructor stub

        createView();
    }

    private void createView() {
        hotSeat = new HotSeatView(mContext);
        this.addView(hotSeat, new LayoutParams(LayoutParams.MATCH_PARENT,
                UIConfig.hotSeatLineHeight, Gravity.BOTTOM));
    }

    public void updateHotSeatList(List<AppItemData> dataList) {
        hotSeat.updateList(dataList);
    }

    public boolean addHotSeatItem(AppItemData data, String path, boolean binit) {
        return hotSeat.addItem(data, path, binit);
    }

    public boolean removeHotSeatItem(final AppItemData data, final String path) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                hotSeat.removeItem(data, path);
            }
        });
        return true;
    }

    private AnimatorListener showHostSeatListener = new AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            // TODO Auto-generated method stub
            hotSeat.resetFocus();
            hotSeat.showArrowTip();
            NormalController.handleKey = true;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub
        }
    };

    // 隐藏左侧菜单之后，显示快捷栏
    public void showHotSeat() {
        NormalController.handleKey = false;
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                try {
                    EasyLauncherActivity.exitSwitch = XServiceManager.getLiteConfigService().getBooleanValue(
                            BackExit.BACK_EXIT, true);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        hotSeat.animate().y(Utils.getScreenHeight(mContext) - hotSeat.getHeight())
                .setDuration(UIConfig.HotSeatShowAnimDuration).setListener(showHostSeatListener);
        hotSeat.setVisibility(View.VISIBLE);
        hotSeat.resetFocus();
    }

    public void saveFocusBeforeHide() {
        hotSeat.markLastFocusView();
    }

    private AnimatorListener hideHostSeatListener = new AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            // TODO Auto-generated method stub
            NormalController.handleKey = true;
            hotSeat.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub
        }
    };

    // 显示左侧菜单时要隐藏快捷栏
    public void hideHotSeat() {
        // hotSeat.markLastFocusView();
        hotSeat.animate().y(Utils.getScreenHeight(mContext))
                .setDuration(UIConfig.HotSeatShowAnimDuration).setListener(hideHostSeatListener);
        GuideViewManager.getInstance().hideMe();
    }

    public void setHotSeatExecutor(HotSeatExecutor hotSeatExecutor) {
        hotSeat.setHotSeatExecutor(hotSeatExecutor);
    }

    public void setBoundaryDetector(BoundaryDetector boundaryDetector) {
        hotSeat.setBoundaryDetector(boundaryDetector);
    }

    public boolean processKeyDown(int keyCode) {
        return hotSeat.processKeyDown(keyCode);
    }

    // 重置快捷栏的焦点位置，在以下两种情况调用：1.隐藏菜单显示快捷栏；2.从全部应用界面回到快捷栏
    public void resetHotSeatFocus() {
        if (hotSeat != null)
            hotSeat.resetFocus();
    }

    public void setPickResultListener(PickResultListener listener) {
        hotSeat.setPickResultListener(listener);
    }

    // 初次启动桌面时，调用快捷栏的initFocus方法来设置焦点
    @Override
    public void resetFocus() {
        // TODO Auto-generated method stub
        if (hotSeat != null)
            hotSeat.initFocus();
    }

    public HotSeatView getHotSeat() {
        return hotSeat;
    }

    public void setHotSeat(HotSeatView hotSeat) {
        this.hotSeat = hotSeat;
    }

    public void destory() {
        if (hotSeat != null)
            hotSeat.destory();
    }

}
